package com.itbac.redis_lock.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

/**
 * 模拟一个仓库数据。
 * @Description:
 * @Auther: BacHe
 * @Date: 2020/8/28 12:56
 */
@RestController
@RequestMapping
public class InfoController {

    private static String moneyKey = "moneyKey";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/getInfo")
    public Integer getInfo(){
        return (Integer) redisTemplate.opsForValue().get(moneyKey);
    }

    @RequestMapping("/setInfo/{value}")
    public String setInfo(@PathVariable int value) {
        redisTemplate.opsForValue().set(moneyKey, value);
        int i = (Integer) redisTemplate.opsForValue().get(moneyKey);
        return "设置成功,当前值:" + i;
    }
    //设置初始值
//    @PostConstruct
//    public void init(){
//        redisTemplate.opsForValue().set(moneyKey, 10000);
//        stringRedisTemplate.opsForValue().set(moneyKey + "_2", "8000");
//    }


}
